package cn.elwy.rcp.ui.explorer.core.dao.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import cn.elwy.common.exception.MsgLevel;
import cn.elwy.rcp.ui.explorer.core.dao.AuditDao;

@Scope("prototype")
@Repository(AuditDao.ID)
public class AuditDaoLoggerImpl implements AuditDao {

  Logger logger = LoggerFactory.getLogger(this.getClass());

  @Override
  public void audit(MsgLevel level, String message) {
    logger(level, message, null);
  }

  @Override
  public void audit(MsgLevel level, String message, Throwable exception) {
    logger(level, message, exception);
  }

  @Override
  public void audit(MsgLevel level, Throwable exception) {
    logger(level, exception.getMessage(), exception);
  }

  private void logger(MsgLevel level, String message, Throwable exception) {
    switch (level) {
    case INFO:
      if (exception == null) {
        logger.info(message);
      } else {
        logger.info(message, exception);
      }
      break;
    case ERROR:
      if (exception == null) {
        logger.error(message);
      } else {
        logger.error(message, exception);
      }
      break;
    case WARN:
      if (exception == null) {
        logger.warn(message);
      } else {
        logger.warn(message, exception);
      }
      break;
    case DEBUG:
      if (exception == null) {
        logger.debug(message);
      } else {
        logger.debug(message, exception);
      }
      break;
    default:
      if (exception == null) {
        logger.error(message);
      } else {
        logger.error(message, exception);
      }
      break;
    }
  }
}
